# 数据库链接
import configparser
import os
import sqlite3
import sys
import traceback

import pymysql

TopPath = os.path.abspath(os.path.dirname(os.path.dirname(os.getcwd())))
CellPath = os.path.join(TopPath, "download", "cell")
FILE_DOWNLOAD_PATH = os.path.join(CellPath, 'download')
DIR_TIME_DETAILS = os.path.join(FILE_DOWNLOAD_PATH, 'abstractdetails')
curPath = os.path.dirname(os.path.abspath(__file__))
selectResult = list()


def MajorDbConnect():
    cf = configparser.ConfigParser()
    try:
        cf.read(os.path.join(curPath, 'db.ini'))
    except:
        print('读取配置失败:' + traceback.format_exc())
        sys.exit(-1)
    if cf:
        db_host = cf.get('db', 'db_host')
        db_user = cf.get('db', 'db_user')
        db_pw = cf.get('db', 'db_pw')
        db_name = cf.get('db', 'db_name')
        db_charset = cf.get('db', 'db_charset')
        conn = pymysql.connect(host=db_host, user=db_user, passwd=db_pw, db=db_name, charset=db_charset)
        if conn:
            return conn


def SelectListFromDB():
    global nCount, ListSqls
    dbMsg = None
    # 数据库连接
    conn = MajorDbConnect()
    if conn:
        cur = conn.cursor()
        sSql = "SELECT `id`,`name`,`url` FROM `archive` where stat=1 and Failed < 5"
        try:
            cur.execute(sSql)
            print(sSql)

        except:
            print('* errSql:' + sSql)
            dbMsg = '*errSynax:' + traceback.format_exc()

        if dbMsg:
            print(dbMsg)
            sys.exit(-1)

        rows = cur.fetchall()
        conn.commit()
        conn.close()
        return rows


# 插入数据库
def SelectIntoDbSqlite():
    global selectResult
    dbMsg = None
    # conn = pyodbc.connect('Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s' %os.path.join(curPath,'wanfang_bz.accdb'))
    conn = sqlite3.connect('cell.db3')
    cur = conn.cursor()
    if conn:
        sql = "select lngid FROM modify_title_info_zt"
        try:
            cur.execute(sql)
        except:
            dbMsg = '*InsertError:' + traceback.format_exc()
        if dbMsg:
            exit(-1)
    selectResult = cur.fetchall()
    listresult = []
    for tuplereslut in selectResult:
        listresult.append(tuplereslut[0])
    selectResult = listresult
    conn.commit()
    conn.close()


def mymain():
    rows = SelectListFromDB()
    archive = []
    dic = {}
    dic2 = {}
    for id, name, url in rows:
        listname = url.split("/")
        names = "CELL_WK_" + listname[-1]
        archive.append(names)
        dic[names] = url
        dic2[names] = name
    SelectIntoDbSqlite()
    print(len(archive))
    print(len(selectResult))
    ret = list(set(archive) ^ set(selectResult))
    print(len(ret))
    print(ret)
    files = []
    filePaths = []
    for key in ret:
        url = dic[key]
        listname = url.split("/")
        # 这三行可以将fulltext改到abstractdetails
        if listname[-2] == "fulltext":
            listname[-2] = "abstract"
        else:
            problem = "倒数第二个不是fulltext，请检查"
            sys.exit(-1)
        print(listname)
        filename = "_".join(listname)
        filename = "{}{}{}".format(dic2[key], filename, ".html")
        print(filename)
        files.append(filename)

    rootdir = DIR_TIME_DETAILS
    for path, dirNames, fileNames in os.walk(rootdir):
        for filename in files:
            filePath = os.path.join(path, filename)
            if not os.path.exists(filePath):
                print("{}文件不存在".format(filePath))
                sys.exit(-1)
            filePaths.append(filePath)
    print(len(filePaths))
    return filePaths
